/* import "reflect-metadata";
import { IsNotEmpty, Max, MaxLength, Min, MinLength, validate } from "class-validator";

class RegUser {
  @IsNotEmpty({ message: "账号不能为空" })
  @MinLength(5, { message: "账号必须至少5个字符" })
  @MaxLength(12, { message: "账号最多12个字符" })
  loginId: string;
  loginPwd: string;

  @Min(0, { message: "年龄最小值是0" })
  @Max(100, { message: "年龄最大值是100" })
  age: number;
  gender: "男" | "女";
}

const post = new RegUser();
post.loginId = "123";
post.age = 111;

validate(post).then((error: any) => {
  console.log(error);
});
 */

import "reflect-metadata";
import { plainToClass, Type } from "class-transformer";
import axios from "axios";

class User {
  id: number;
  firstName: string;
  lastName: string;

  @Type(() => Number)
  age: number;

  getName() {
    return this.firstName + " " + this.lastName;
  }

  isAdult() {
    return this.age > 36 && this.age < 60;
  }
}

axios
  .get("https://www.myjsons.com/v/7ec14288")
  .then((resp) => resp.data)
  .then((users) => {
    if (!Array.isArray(users)) {
      users = [];
    }
    // 将平面数组转换为数组
    const us = plainToClass(User, users) as unknown as User[];
    console.log("🚀 ~ .then ~ us:", us);
    for (const u of us) {
      // 将平面对象转换为对象
      // const user = plainToClass(User, u);
      console.log(u.getName(), u.isAdult());
      console.log(u.age, typeof u.age);
    }
  });
